package com.dataextend.controller;

import com.alibaba.fastjson.JSONObject;
import com.dataextend.dto.RequestList;
import com.dataextend.dto.ResultSegment;
import com.dataextend.entity.*;
import com.dataextend.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *   用户画像模型
 */
@RestController
@RequestMapping("/v1/UserProfile")
public class UserProfileContorller {
    @Autowired
    private UserGroupCalculateService userGroupCalculateService;
//    @Autowired
//    private UserGroupAnalysisService userGroupAnalysisService;
    @Autowired
    private UserProfileService userProfileService;
    @Autowired
    private EventListService eventListService;
//    @Autowired
//    private UserTagsService userTagsService;
    @Autowired
    private UserTagsServiceV2 userTagsServiceV2;
    @Autowired
    private UserActTimelineService userActTimelineService;

    /**
     *  首页查询用户标签视图
     */
    @RequestMapping(value = "/searchUserTagView", method = RequestMethod.POST)
    public ResultSegment userTagView() {
        JSONObject result =  userProfileService.userTagView();
        if (result != null){
            return new ResultSegment(true,1,"查询成功", result);
        } else {
            return new ResultSegment(false,400,"程序异常");
        }
    }


    /**
     *  标签列表 —— 根据标签ID进行查询
     */
    @RequestMapping(value = "/searchTagId", method = RequestMethod.POST)
    public ResultSegment searchTagId(@RequestParam(value = "tagId", required = false) String tagId){
        JSONObject result = userProfileService.searchTagId(tagId);
        if (result != null){
            return new ResultSegment(true,1,"查询成功", result);
        } else {
            return new ResultSegment(false,400,"程序异常");
        }
    }


    /**
     *  标签数量校验
     */
    @RequestMapping(value = "/checkTagNum", method = RequestMethod.POST)
    public ResultSegment checkTagNum(@RequestParam(value = "tagId", required = false) String tagId,
                                     @RequestParam(value = "startDate", required = false) String startDate,
                                     @RequestParam(value = "endDate", required = false) String endDate){
        JSONObject result = userProfileService.checkTagNum(tagId, startDate, endDate);
        if (result != null){
            return new ResultSegment(true,1,"查询成功", result);
        } else {
            return new ResultSegment(false,400,"程序异常");
        }
    }


    /**
     *  同二级大类下各标签所占比例
     */
    @RequestMapping(value = "/checkTagRatio", method = RequestMethod.POST)
    public ResultSegment checkTagRatio(@RequestParam(value = "tagId", required = false) String tagId,
                                     @RequestParam(value = "dataDate", required = false) String dataDate){
        JSONObject result = userProfileService.checkTagRatio(tagId, dataDate);
        if (result != null){
            return new ResultSegment(true,1,"查询成功", result);
        } else {
            return new ResultSegment(false,400,"程序异常");
        }
    }




    /**
     *  标签列表 —— 展示
     */
    @RequestMapping(value = "/UserTagList", method = RequestMethod.POST)
    public ResultSegment listuserTag(@RequestBody RequestList requestList) {
        JSONObject map = userProfileService.listUserTag(requestList.getPageNo(), requestList.getPageSize());
        return new ResultSegment(true, 1, "查询成功", map);
    }


    /**
     *  添加用户标签
     */
//    @RequiresPermissions("usertag:permission:add")
    @RequestMapping(value = "/addUserTag", method = RequestMethod.POST)
    public  JSONObject AddUser(@RequestBody UserProfileTag userProfileTag){
        return userProfileService.addUserTag(userProfileTag);
    }


    /**
     *  修改用户标签
     */
//    @RequiresPermissions("usertag:permission:update")
    @RequestMapping(value = "/updateUserTag", method = RequestMethod.POST)
    public JSONObject updateUserTag(@RequestBody UserProfileTag userProfileTag){
        return userProfileService.updateUserTag(userProfileTag);
    }


    /**
     *  删除用户标签
     */
//    @RequiresPermissions("usertag:permission:delete")
    @RequestMapping(value = "/deleteUserTag", method = RequestMethod.POST)
    public JSONObject deleteUserTag(@RequestParam(value = "tag_id", required = false) String tag_id){
        return userProfileService.deleteUserTag(tag_id);
    }


    /**
     *  标签列表 —— 根据标签名称进行查询
     */
    @RequestMapping(value = "/searchUserTag", method = RequestMethod.POST)
    public ResultSegment searchUserTag(@RequestParam(value = "tagname", required = false) String tagname){
        JSONObject map = userProfileService.searchUserTag(tagname);
        return new ResultSegment(true, 1, "查询成功", map);
    }



    /**
     *  用户画像 —— 标签筛选用户标签下拉1 2 3级菜单
     */
    @RequestMapping(value = "/usergroup/dropDownList1", method = RequestMethod.POST)
    public ResultSegment dropDownList1(@RequestParam(value = "level1", required = false) String level1) {
        JSONObject map =  eventListService.dropDownList1(level1);
        return new ResultSegment(true, 1, "查询成功", map);
    }

    @RequestMapping(value = "/usergroup/dropDownList2", method = RequestMethod.POST)
    public ResultSegment dropDownList2(@RequestParam(value = "level1", required = false) String level1,
                                    @RequestParam(value = "level2", required = false) String level2) {
        JSONObject map =  eventListService.dropDownList2(level1, level2);
        return new ResultSegment(true, 1, "查询成功", map);
    }



//    /**
//     *  用户标签 —— 用户全量标签查询
//     */
//    @RequestMapping(value = "/userTagSearch", method = RequestMethod.POST)
//    public ResultSegment userTagSearch(@RequestBody RequestUserTag requestUserTag){
//        JSONObject userTag = userTagsService.findUser(requestUserTag);
//        return new ResultSegment(true, 1, "查询成功", userTag);
//    }


    /**
     *  用户标签 —— 用户全量标签查询修改es的标签数据结构 /v2
     */
    @RequestMapping(value = "/userTagSearch", method = RequestMethod.POST)
    public ResultSegment userTagSearchV2(@RequestBody RequestUserTag requestUserTag){
        JSONObject userTag = userTagsServiceV2.findUser(requestUserTag);
        return new ResultSegment(true, 1, "查询成功", userTag);
    }



    /**
     *  用户标签 —— 用户行为路径查询
     */
    @RequestMapping(value = "/userActTimelineSearch", method = RequestMethod.POST)
    public ResultSegment userActTimelineSearch(@RequestBody RequestUserTag requestUserTag){
        JSONObject userTag = userActTimelineService.findUser(requestUserTag);
        return new ResultSegment(true, 1, "查询成功", userTag);
    }




}
